package com.dream.testtubebaby_doctor.adapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.dream.testtubebaby_doctor.AddToldActivity;
import com.dream.testtubebaby_doctor.R;
import com.dream.testtubebaby_doctor.data.ToldPatientItem;

public class ToldPatientAdapter extends BaseAdapter implements SectionIndexer, Filterable{

	private List<ToldPatientItem> list = null;
	private Context mContext;
	private AddDoctorListener mAddDoctorListener;
	private ItemInfoFilter filter;
	public interface AddDoctorListener{
		void onNotifyAddDoctor(int position, long uid);
	}
	
	public void setmAddDoctorListener(AddDoctorListener addDoctorListener){
		mAddDoctorListener = addDoctorListener;
	}
	
	public ToldPatientAdapter(Context mContext, List<ToldPatientItem> list) {
		this.mContext = mContext;
		this.list = list;
	}

	public void updateListView(List<ToldPatientItem> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public int getCount() {
		return this.list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		final ToldPatientItem mContent = list.get(position);
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(mContext).inflate(
					R.layout.told_patient_item_view, null);
			viewHolder.told_patient_name = (TextView) view
					.findViewById(R.id.told_patient_name_tv);
			viewHolder.told_patient_age = (TextView) view
					.findViewById(R.id.told_patient_age_tv);
			viewHolder.told_patient_address = (TextView) view
					.findViewById(R.id.told_patient_address_tv);
			viewHolder.told_patient_dsc = (TextView) view
					.findViewById(R.id.told_patient_dsc_tv);
			viewHolder.told_patient_vip = (TextView) view
					.findViewById(R.id.told_patient_vip_tv);
			viewHolder.told_patient_filing = (TextView) view
					.findViewById(R.id.told_patient_filing_tv);
			viewHolder.told_patient_addtold = (TextView) view
					.findViewById(R.id.told_patient_add_told_tv);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		if (mContent.getType() == 1) {
			viewHolder.told_patient_name.setText(mContent.getName());
			viewHolder.told_patient_vip.setVisibility(View.GONE);
			viewHolder.told_patient_dsc.setVisibility(View.GONE);
			viewHolder.told_patient_age.setVisibility(View.GONE);
			viewHolder.told_patient_address.setVisibility(View.GONE);
			viewHolder.told_patient_filing.setVisibility(View.GONE);
			viewHolder.told_patient_addtold.setText(R.string.apply_patient);
			viewHolder.told_patient_addtold.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mAddDoctorListener != null){
						long uid = mContent.getUserid();
						mAddDoctorListener.onNotifyAddDoctor(position, uid);
					}
				}
			});
		} else {
			viewHolder.told_patient_vip.setVisibility(View.GONE);
			viewHolder.told_patient_dsc.setVisibility(View.GONE);
			viewHolder.told_patient_age.setVisibility(View.GONE);
			viewHolder.told_patient_address.setVisibility(View.GONE);
			viewHolder.told_patient_filing.setVisibility(View.GONE);
			viewHolder.told_patient_name.setText(mContent.getName());
//			viewHolder.told_patient_age.setText(mContent.getAge());
//			viewHolder.told_patient_address.setText(mContent.getAddress());
//			viewHolder.told_patient_dsc.setText(mContent.getDec());
//			viewHolder.told_patient_vip.setText(mContent.getVip());
//			viewHolder.told_patient_filing.setText(mContent.getFiling());
			viewHolder.told_patient_addtold.setText(R.string.add_told_title);
			viewHolder.told_patient_addtold
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent intent = new Intent(
									"com.dream.add.told.action");
							intent.putExtra(AddToldActivity.TOLD_DETAIL_KEY,
									mContent);
							mContext.startActivity(intent);
						}
					});
		}
		return view;

	}

	final static class ViewHolder {
		TextView told_patient_name;
		TextView told_patient_age;
		TextView told_patient_address;
		TextView told_patient_dsc;
		TextView told_patient_vip;
		TextView told_patient_filing;
		TextView told_patient_addtold;
	}

	public int getSectionForPosition(int position) {
		return list.get(position).getSortLetters().charAt(0);
	}

	@SuppressLint("DefaultLocale")
	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = list.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}

		return -1;
	}

	@Override
	public Object[] getSections() {
		return null;
	}

	@Override
	public Filter getFilter() {
		if (filter == null)
			filter = new ItemInfoFilter(list);
		return filter;
	}
	private class ItemInfoFilter extends Filter {

		protected android.widget.Filter.FilterResults performFiltering(
				CharSequence charsequence) {
			android.widget.Filter.FilterResults filterresults = new android.widget.Filter.FilterResults();
			String s = charsequence.toString().replaceAll(" ", "")
					.toLowerCase().trim();
			if (charsequence == null || charsequence.length() == 0) {
				filterresults.values = original;
				filterresults.count = original.size();
			} else {
				ArrayList<ToldPatientItem> arraylist = new ArrayList<ToldPatientItem>();
				Iterator<?> iterator = original.iterator();
				do {
					if (!iterator.hasNext())
						break;
					ToldPatientItem addToldData = (ToldPatientItem) iterator.next();
					if (addToldData.getName() != null
							&& addToldData.getName().toLowerCase()
									.replaceAll(" ", "").startsWith(s))
						arraylist.add(addToldData);
				} while (true);
				filterresults.values = arraylist;
				filterresults.count = arraylist.size();
			}
			return filterresults;
		}

		@SuppressWarnings("unchecked")
		protected void publishResults(CharSequence charsequence,
				android.widget.Filter.FilterResults filterresults) {
			list = (List<ToldPatientItem>) filterresults.values;
			notifyDataSetChanged();
		}

		private List<?> original;

		public ItemInfoFilter(List<?> list) {
			original = list;
		}
	}
}
